#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 20;
int n, t, cnt, s[N], c[2521], g[N][2525];
ll f[N][200][2521];

ll solve(int x, bool lim, int rest, int mod) {
  if (!x) return !(rest % mod);
  if (!lim && f[x][c[mod]][rest] != -1) return f[x][c[mod]][rest];
  ll res = 0;
  int maxx = lim ? s[x] : 9;
  for (int i = 0; i <= maxx; ++i)
    res += solve(x - 1, lim && i == maxx, x > 1 ? (rest * 10 + i) % 252 : rest * 10 + i,
                 g[i][mod]);
  return lim ? res : f[x][c[mod]][rest] = res;
}

inline ll solve(ll x) {
  cnt = 0;
  while (x) s[++cnt] = x % 10, x /= 10;
  return solve(cnt, true, 0, 1);
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("num.in", "r", stdin);
  freopen("num.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  memset(f, -1, sizeof(f));
  for (int i = 1, rest = -1; i <= 2520; ++i) c[i] = rest += !(2520 % i);
  for (int i = 0; i < 10; ++i) {
    for (int j = 1; j <= 2520; ++j) g[i][j] = i ? i * j / gcd(i, j) : j;
  }
  for (ll l, r; t; --t) {
    scanf("%lld%lld", &l, &r);
    printf("%lld\n", solve(r) - solve(l - 1));
  }
  return 0;
}
